﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gfw.BLL.Basic.Site;
using Gfw.Web.Common;
using Gfw.Web.Models;
using Gfw.Web.Models.Site;

namespace Gfw.Web.Controllers
{
     [LAuthorize]
    public class AnnouncementController : Controller
    {
        /// <summary>
        /// 当前选择的页数
        /// </summary>
        /// 如果是选择参数，也可以这样处理
        int PageIndex
        {
            get { return Session["AnnouncementController_PageIndex"].ConvertTo<int>(1); }
            set { Session["AnnouncementController_PageIndex"] = value; }
        }

        private AnnouncementQueryModel QueryModel
        {
            get { return Session["AnnouncementController_AnnouncementQueryModel"].ConvertTo<AnnouncementQueryModel>(null); }
            set { Session["AnnouncementController_AnnouncementQueryModel"] = value; }
        }

        /// <summary>
        /// 最新十条公告
        /// </summary>
        /// <returns></returns>
        public ActionResult GetLatestAnnouncements()
        {
            var list = AnnouncementLogic.GetLatestAnnouncements();
            return View(list);
        }

        //
        // GET: /Admin/Announcement/
        /// <summary>
        /// 查找公告
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(AnnouncementQueryModel model)
        {
            QueryModel = model;
            PageIndex = 1;
            return View(model);
        }

        /// <summary>
        /// 返回指定页面
        /// </summary>
        /// <param name="page">重第一页开始计算</param>
        /// <returns></returns>
        public ActionResult List(int? page)
        {
            int totalSize;
            int index = page ?? PageIndex;
            if (index < 1) index = 1;

            var list = AnnouncementLogic.Query(true, QueryModel.Key, QueryModel.Start, QueryModel.End, index - 1, ViewHelper.PageSize, out totalSize);
            ViewBag.TotalSize = totalSize;
            PageIndex = ViewBag.PageIndex = index;

            return PartialView(list);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isDefault">true: 回到首页</param>
        /// <returns></returns>
        public ActionResult Detail(string id, bool isDefault)
        {
            AnnouncementModel model = AnnouncementLogic.GetAnnouncementByID(id);
            if (model == null)
                return HttpNotFound();

            ViewBag.isDefault = isDefault;
            ViewBag.QueryModel = QueryModel;

            return View(model);
        }
    }
}
